package com.cogent.dao.type;

import com.cogent.common.util.StringUtil;

public class DistrictControl {
	public static String getStaticControl(String district, String type) {
		return getStaticControl("districtcode", district, type);
	}
	
	public static String getStaticControl(String filed, String district, String type) {
		int level = getDistrictLevel(district);
		String ret = "stroffice";
		if(type.equals("CASCADE")) {
			if(level < 5)
				ret = "communitee";
		}else if(type.equals("DISTRCASCADE")) {
			if(level >4)
				ret = "concat(substr(" + filed + ", 0, 9), '000')";
			else
				ret = filed;
		}else
			ret = "" + (level - 4) * 3;
		
		return ret;
	}
	
	public static String getControlCode(String district) {
		return getControlCode(district, true);
	}
	
	public static String getControlCode(String district, boolean isomit) {
		String ret = district;
		if(isomit && (isOmitSearch(district)))
			ret = "";
		else {
			int level = getDistrictLevel(district);
			if(level > 3)
				ret = district.substring(0, district.length() - (level -3)*3);
		}
		
		return ret;
	}
	
	public static int getQueryType(String district) {
		int ret = QueryType.EQUAL;
		int level = getDistrictLevel(district);
		if(level > 3)
			ret = QueryType.LEFTMATCH;
		
		return ret;
	}
	
	public static boolean isOmitSearch(String district) {
		int level = getDistrictLevel(district);
		boolean flag = false;
		if(level == 5)
			flag = true;
		
		return flag;
	}
	
	public static String getQueryField(String district) {
		int level = getDistrictLevel(district);
		String ret = "communitee";
		if(level == 5)
			ret = "stroffice";
		
		return ret;
	}
	
	public static int getDistrictLevel(String district) {
		int level = 5;
		String temp = district.substring(district.length()-3);
		if(StringUtil.toInt(temp) > 0)
			level = 3;
		else {
			temp = district.substring(6, 9);
			if(StringUtil.toInt(temp) > 0)
				level = 4;
		}
		
		return level;
	}
}
